package riteh.eventNetwork.client;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;

public class ActivitySearch extends ListActivity {

	private Button btnSearch;
	private EditText editSearch;
	private String query;
	private SearchAdapter adapter;
	private long userId;
	private ListView list;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(C.TAG, "SEARCH Activity");
		setContentView(R.layout.search);
		
	// INSTANCIRANJE OBJEKTA
		btnSearch = (Button) findViewById(R.id.searchButtonSearch);
		editSearch = (EditText) findViewById(R.id.searchEditSearch);
		
	// AKCIJE NA STARTU
		userId = getIntent().getExtras().getLong("userId");
		query = getIntent().getExtras().getString("query");
		
		list = getListView();
		registerForContextMenu(list);
		
		if (userId == C.TEST_ID) {
			String users[][] = C.SEARCH_USERS;
			adapter = new SearchAdapter(this, users);
			setListAdapter(adapter);
		}
		else {
			searchUsers();			
		}
		
		setResult(C.ERROR);
	
	// EVENT LISTENERS
		
		btnSearch.setOnClickListener( new OnClickListener() {
			
			public void onClick(View v) {
				if (userId == C.TEST_ID) return;
				query = editSearch.getText().toString();
				searchUsers();
			}
		});
	}
	
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = new MenuInflater(this);
		inflater.inflate(R.menu.long_click_search, menu);
	}
	
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		View v = info.targetView;
		long selectionId = v.getId();
		switch (item.getItemId()) {
		case R.id.longClickSearchFrendshipRequest:
			sendFriendshipRequest(selectionId);
			return true;
		}
		return false;
	}

/* ********************** PRIVATE FUNCTIONS *******************************/	

	private void sendFriendshipRequest(long selUserId) {
		RestClient client = new RestClient(getString(R.string.serverMethodSendFriendshipRequest));
		client.AddParam("userId", String.valueOf(userId));
		client.AddParam("selectedUserId", String.valueOf(selUserId));
		
		String result = C.getMethod0(client, getString(R.string.serverMethodSendFriendshipRequest));
		
		if (C.resultSuccess(this, result,getString(R.string.toastSendFriendshipRequestError))) {
			C.toast(this, "Friendship request sent!",0);
			setResult(C.SUCCESS);
		}

	}
	
	private void searchUsers() {
		Log.d(C.TAG, "SEARCH : searchUsers()");
		
    	if (query.isEmpty()) {
    		C.toast(this, "Enter search keyword!");
    		return;
    	}
		
		RestClient client = new RestClient(getString(R.string.serverMethodGetPeople));
		client.AddParam("userId", String.valueOf(userId));
		client.AddParam("nameQuery", query);
		
		String result[][] = C.getMethod2(client, "getPeople");
		
		if (C.resultSuccess(this, result, getString(R.string.toastSearchError))) {
			
			if (result[0][0].compareTo("0") == 0) return;
			
			String users[][] = result;
			adapter = new SearchAdapter(this, users);
			setListAdapter(adapter);
		}
		
	}
	
	private class SearchAdapter extends ArrayAdapter<String[]> {
		private final Activity context;
		private final String[][] users;

		public SearchAdapter(Activity context, String[][] users) {
			super(context, R.layout.search_list, users);
			this.context = context;
			this.users = users;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = context.getLayoutInflater();
			View rowView = inflater.inflate(R.layout.search_list, null, true);
			
			TextView name = (TextView) rowView.findViewById(R.id.searchListLabelName);
			TextView email = (TextView) rowView.findViewById(R.id.searchListLabelEmail);

			rowView.setId(Integer.parseInt(users[position][0]));
			name.setText(users[position][1] + " " + users[position][2]);
			email.setText(users[position][3]);

			return rowView;
		}
	}
}
